package org.ielse.frame.test;

import java.util.ArrayList;
import java.util.List;

import org.ielse.frame.BaseActivity;
import org.ielse.frame.BaseAdapter;
import org.ielse.frame.R;
import org.ielse.frame.t.obsessive.zbar.CaptureActivity;
import org.ielse.frame.t.view.ultra.PtrDefaultHandler;
import org.ielse.frame.t.view.ultra.PtrFrameLayout;
import org.ielse.frame.t.view.ultra.PtrHandler;
import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.util.LogUtil;
import org.ielse.frame.util.NetworkUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class RefreshActivity extends BaseActivity {

	// private ElasticFrameLayout layRefresh;
	private ListView viewList;
	private final List<String> dataList = new ArrayList<String>();

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_refresh);
		super.onCreate(savedInstanceState);

		viewList = (ListView) findViewById(R.id.view_list);

		viewList.setOnItemClickListener(new OnItemClickListener() {
			@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (position < 10) {
					startActivity(Refresh2Activity.class);
				}
				else {
					startActivity(CaptureActivity.class);
				}

			}
		});
		viewList.setAdapter(adapter);

		for (int i = 0; i < 30; i++) {
			dataList.add("abc");
		}
		adapter.notifyDataSetChanged();

		final PtrFrameLayout mPtrFrameLayout = (PtrFrameLayout) findViewById(R.id.material_style_ptr_frame);
		// header
		// final StoreHouseHeader header = new StoreHouseHeader(context);
		// header.setTextColor(0xff666666);
		// header.setPadding(0, BaseUtil.dipToPx(15), 0, 0);
		// header.initWithPointList(RefreshPoint.getLoadingText());

		final View header = new View(context);
		header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0));
		mPtrFrameLayout.setDurationToCloseHeader(1500);
		mPtrFrameLayout.setHeaderView(header);
		// mPtrFrameLayout.addPtrUIHandler(header);
		mPtrFrameLayout.postDelayed(new Runnable() {
			@Override public void run() {
				mPtrFrameLayout.autoRefresh(false);
			}
		}, 100);

		mPtrFrameLayout.setPtrHandler(new PtrHandler() {
			@Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame, viewList, header);
				// return true;
			}

			@Override public void onRefreshBegin(final PtrFrameLayout frame) {
				frame.refreshComplete();
			}
		});

	}

	private BaseAdapter adapter = new BaseAdapter(dataList) {
		@Override public View getView(int position, View convertView, ViewGroup parent) {
			TextView txTitle = new TextView(context);
			txTitle.setId(R.id.tx_title);
			txTitle.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			txTitle.setText("title:" + position);
			if(position > 10) {
				txTitle.setText("qrcode_scan~");	
			}
			return txTitle;
		}
	};
}
